package com.koron.common.core.util.sms;

import org.apache.axis2.AxisFault;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MobileMessageSenderImpl
 * @Date 2019/6/17 11:59
 * @Description 短信发送
 **/
@Component
public class MobileMessageSenderImpl implements IMobileMessageSender {

    private static Logger LOGGER = LoggerFactory.getLogger(MobileMessageSenderImpl.class);

    /**
     * 发短信
     *
     * @param mobileMessages
     * @return
     */
    @Override
    public int sendMessage(List<MobileMessageVO> mobileMessages) {
        if (CollectionUtils.isEmpty(mobileMessages)) {
            LOGGER.warn("没有要发送的短信");
            return 0;
        }

        SmsServiceStub stub = null;
        try {
            stub = new SmsServiceStub();
        } catch (AxisFault e1) {
            LOGGER.error("短信服务初始化失败", e1);
            return 0;
        }

        int count = 0;
        List<MobileMessageVO> successList = new ArrayList<>();
        try {
            String phone = null;
            String content = null;
            SmsServiceStub.InsertDownSms insertDownSms = null;
            for (int i = 0; i < mobileMessages.size(); i++) {
                phone = mobileMessages.get(i).getReceiverPhone();
                content = mobileMessages.get(i).getMessageContent();
                insertDownSms = getInsertDownSms(phone, content);
                SmsServiceStub.InsertDownSmsResponse response = stub.insertDownSms(insertDownSms);
                LOGGER.info(response.getInsertDownSmsReturn().getString());
                count++;
                successList.add(mobileMessages.get(i));
            }
        } catch (RemoteException e) {
            LOGGER.error("短信发送失败", e);
            throw new RuntimeException("短信发送失败");
        }

        LOGGER.info("短信发送完成，共发送{}条", count);
        return count;
    }

    @Override
    public int sendSingleMessage(MobileMessageVO mobileMessages) {
        List<MobileMessageVO> list = new ArrayList<>();
        list.add(mobileMessages);
        return sendMessage(list);
    }

    /**
     * 封装短信实体
     *
     * @param phone
     * @param content
     * @return
     */
    private SmsServiceStub.InsertDownSms getInsertDownSms(String phone, String content) {
        SmsServiceStub.InsertDownSms insertDownSms = new SmsServiceStub.InsertDownSms();
        org.apache.axis2.databinding.types.soapencoding.String str = new org.apache.axis2.databinding.types.soapencoding.String();
        str.setString("Aj8=");
        insertDownSms.setUsername(str);
        str = new org.apache.axis2.databinding.types.soapencoding.String();
        str.setString("Am4DOl1kUnFXNVAhU2tXJw==");
        insertDownSms.setPassword(str);
        str = new org.apache.axis2.databinding.types.soapencoding.String();
        str.setString("0002");
        insertDownSms.setBatch(str);
        str = new org.apache.axis2.databinding.types.soapencoding.String();
        str.setString("<sendbody><message><orgaddr></orgaddr><mobile>" + phone + "</mobile><content>" + content + "</content><sendtime></sendtime></message><publicContent></publicContent></sendbody>");
        insertDownSms.setSendbody(str);

        return insertDownSms;
    }
}
